/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.phone.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.phone.entity.PhoneCommodity;
import com.jeesite.modules.phone.entity.Staff;
import com.jeesite.modules.phone.service.PhoneCommodityService;
import com.jeesite.modules.phone.service.StaffService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.phone.entity.ReturnGoods;
import com.jeesite.modules.phone.service.ReturnGoodsService;

/**
 * 退货Controller
 * @author lzw
 * @version 2019-04-03
 */
@Controller
@RequestMapping(value = "${adminPath}/phone/returnGoods")
public class ReturnGoodsController extends BaseController {

	@Autowired
	private ReturnGoodsService returnGoodsService;
	@Autowired
	private PhoneCommodityService phoneCommodityService;
	@Autowired
	private StaffService staffService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ReturnGoods get(Long id, boolean isNewRecord) {
		return returnGoodsService.get(id+"", isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("phone:returnGoods:view")
	@RequestMapping(value = {"list", ""})
	public String list(ReturnGoods returnGoods, Model model) {
		model.addAttribute("returnGoods", returnGoods);
		model.addAttribute("staffList",staffService.findList(new Staff()));
		return "modules/phone/returnGoodsList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("phone:returnGoods:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ReturnGoods> listData(ReturnGoods returnGoods, HttpServletRequest request, HttpServletResponse response) {
		Page<ReturnGoods> page = returnGoodsService.findPage(new Page<ReturnGoods>(request, response), returnGoods); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("phone:returnGoods:view")
	@RequestMapping(value = "form")
	public String form(ReturnGoods returnGoods, Model model) {
		model.addAttribute("returnGoods", returnGoods);
		model.addAttribute("phoneCommodityList",phoneCommodityService.findList(new PhoneCommodity()));
		model.addAttribute("staffList",staffService.findList(new Staff()));
		return "modules/phone/returnGoodsForm";
	}

	/**
	 * 保存退货
	 */
	@RequiresPermissions("phone:returnGoods:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ReturnGoods returnGoods) {
		returnGoodsService.save(returnGoods);
		return renderResult(Global.TRUE, "保存退货成功！");
	}
	
	/**
	 * 停用退货
	 */
	@RequiresPermissions("phone:returnGoods:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(ReturnGoods returnGoods) {
		returnGoods.setStatus(ReturnGoods.STATUS_DISABLE);
		returnGoodsService.updateStatus(returnGoods);
		return renderResult(Global.TRUE, "停用退货成功");
	}
	
	/**
	 * 启用退货
	 */
	@RequiresPermissions("phone:returnGoods:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(ReturnGoods returnGoods) {
		returnGoods.setStatus(ReturnGoods.STATUS_NORMAL);
		returnGoodsService.updateStatus(returnGoods);
		return renderResult(Global.TRUE, "启用退货成功");
	}
	
	/**
	 * 删除退货
	 */
	@RequiresPermissions("phone:returnGoods:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ReturnGoods returnGoods) {
		returnGoodsService.delete(returnGoods);
		return renderResult(Global.TRUE, "删除退货成功！");
	}
	
}